
global data_folder_final "W:\intimate\data"
global log_folder "W:\intimate\dofiles\logs"
global result_folder "W:\intimate\results_revision"

use "W:\intimate\data\match_victim_data_clean_new", clear
drop spouse* data_group

merge m:1 shnro year_start_cohab using "W:\intimate\data\allcouple_data_clean_addfirm", keepusing (employee* earnings* newhire* plant_closed_year* switch*)
drop if _merge==2
drop _merge 

gen turnover_rateB5=-switchB5/employeeB5
gen turnover_rateB4=-switchB4/employeeB4
gen turnover_rateB3=-switchB3/employeeB3
gen turnover_rateB2=-switchB2/employeeB2
gen turnover_rateB1=-switchB1/employeeB1
gen turnover_rate0=-switch0/employeeB1
gen turnover_rateF1=-switchF1/employeeB1
gen turnover_rateF2=-switchF2/employeeB1
gen turnover_rateF3=-switchF3/employeeB1
gen turnover_rateF4=-switchF4/employeeB1
gen turnover_rateF5=-switchF5/employeeB1


// change into long format
keep shnro year_start_cohab dv_couple employee* earnings* newhire* plant_closed_year* turnover_rate* match_id ///
			age seutukunta16* educ ptoim1*
local varlist  employee earnings newhire plant_closed_year turnover_rate

replace plant_closed_yearB5=plant_closed_yearB1 if plant_closed_yearB5==.
replace plant_closed_yearB4=plant_closed_yearB1 if plant_closed_yearB4==.
replace plant_closed_yearB3=plant_closed_yearB1 if plant_closed_yearB3==.
replace plant_closed_yearB2=plant_closed_yearB1 if plant_closed_yearB2==.
replace plant_closed_yearB1=plant_closed_yearB1 if plant_closed_yearB1==.
replace plant_closed_year0=plant_closed_yearB1 if plant_closed_year0==.
replace plant_closed_yearF1=plant_closed_yearB1 if plant_closed_yearF1==.
replace plant_closed_yearF2=plant_closed_yearB1 if plant_closed_yearF2==.
replace plant_closed_yearF3=plant_closed_yearB1 if plant_closed_yearF3==.
replace plant_closed_yearF4=plant_closed_yearB1 if plant_closed_yearF4==.
replace plant_closed_yearF5=plant_closed_yearB1 if plant_closed_yearF5==.

g emp_0 = ptoim10
g emp_1 = ptoim1B1

local varlist  employee earnings newhire plant_closed_year turnover_rate seutukunta16 ptoim1

forvalues i = 1/5 {
    local j = 6 -`i'
    foreach thing in `varlist' {
		ren `thing'B`i' `thing'`j'
	}
}


foreach thing in `varlist' {
    ren `thing'0 `thing'6
}

forvalues i = 1/5 {
    local j = 6 +`i'
    foreach thing in `varlist' {
		ren `thing'F`i' `thing'`j'
	}
}

gen group_id = _n

local varlist  employee earnings newhire plant_closed_year turnover_rate seutukunta16 ptoim1
reshape long `varlist', i(group_id) j(time)

replace turnover_rate=. if missing(turnover_rate)==0 & turnover_rate>2
rename employee firm_size
replace firm_size=10000 if firm_size>10000 & firm_size~=.
replace turnover_rate=0 if turnover_rate<0

gen plant_close=(year>=plant_closed_year)

// Add in Outside Options

g male=0
g time_ = time-6
g year = year_start_cohab + (time - 6)

rename educ level 
gen educ=1 if level<3
replace educ=2 if leve>=3 & level<=5
replace educ=3 if level>=6

rename age age_temp
gen age = floor(age_temp/5)

merge m:1 year educ seutukunta16 age male using "$data_folder_final\munic_edu_age_gender_oo", keepusing(emp obs)
drop if _merge==2 
drop _merge 

gen emp_rate_lo=(emp-ptoim1)/(obs-1)
cap drop obs emp 

// DiD


gen treat= dv_couple==1


*Time displacement dummies
g dpl_5=time_==-5 & treat==1
g dpl_4=time_==-4 & treat==1
g dpl_3=time_==-3 & treat==1
g dpl_2=time_==-2 & treat==1
g dpl_1=time_==-1 & treat==1
g dpl_0=time_==0 & treat==1
gen dpl1=time_==1 & treat==1
gen dpl2=time_==2 & treat==1
gen dpl3=time_==3 & treat==1
gen dpl4=time_==4 & treat==1
gen dpl5=time_==5 & treat==1

gen treatPost= treat==1 & time_>=0
egen full_fe = group(match_id time)
global dummies =  "dpl_5 dpl_4 dpl_3 dpl_2 dpl_0 dpl1 dpl2 dpl3 dpl4 dpl5"
global fe = "full_fe year_start_cohab" 
global cluster = "match_id"


est clear 

    reghdfe ptoim1  treatPost treat,  absorb($fe)  cluster($cluster)
	est sto main_effect
	su ptoim1 if treat==1 & time_<0
	local pmean = round(r(mean), 0.001)
	estadd local premean "`pmean'"
	
	   reghdfe ptoim1  treatPost treat if emp_0==1,  absorb($fe)  cluster($cluster)
	est sto main_effect_emp0
	su ptoim1 if treat==1 & time_<0 & emp_0==1
	local pmean = round(r(mean), 0.001)
	estadd local premean "`pmean'"


foreach thing in earnings firm_size turnover_rate emp_rate_lo {
    reghdfe `thing'  treatPost treat,  absorb($fe)  cluster($cluster)
	est sto robust_`thing'
	su `thing' if treat==1 & time_<0
	local pmean = round(r(mean), 0.001)
	estadd local premean "`pmean'"
}

esttab main_effect main_effect_emp1 main_effect_emp0 robust_* using "$result_folder\victim_external_shock.tex", replace b(4) se(4) r2(4) ///
			keep(treatPost treat) scalars("premean Dependent Mean")


